﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using XXZPrint.Models.Entity;

namespace XXZPrint.Models.DataOperation
{
    public class C_Do_Menu
    {
        /// <summary>
        /// 获取所有的菜单对象
        /// </summary>
        /// <returns></returns>
        public List<menu> GetMenuList()
        {
            using (C_DbContext iDbContext=new C_DbContext())
            {
                return iDbContext.DbContext.Query<menu>().ToList();
            }
        }

        /// <summary>
        /// 获取顶级菜单对象
        /// </summary>
        /// <returns></returns>
        public List<menu> GetTopMenuList()
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<menu>()
                    .Where(w => w.m_pid == 0)
                    .ToList();
            }
        }

        /// <summary>
        /// 获取指定父级ID的菜单对象
        /// </summary>
        /// <param name="m_pid">父级ID</param>
        /// <returns></returns>
        public List<menu> GetMenuListForP(int m_pid)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<menu>()
                    .Where(w => w.m_pid == m_pid)
                    .ToList();
            }
        }

        /// <summary>
        /// 获取指定的菜单对象
        /// </summary>
        /// <param name="id">数据ID</param>
        /// <returns></returns>
        public menu GetMenu(int id)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<menu>()
                    .Where(w => w.m_id == id)
                    .First();
            }
        }

        public int MenuEdit(menu data)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                if (data.m_id == 0)
                {
                    data = iDbContext.DbContext.Insert<menu>(data);
                    return data == null ? 0 : 1;
                }
                else
                {
                    return iDbContext.DbContext.Update<menu>(data);
                }
            }
        }

        /// <summary>
        /// 获取指定菜单ID的菜单文档数据
        /// </summary>
        /// <param name="id">菜单ID</param>
        /// <returns></returns>
        public List<V_MenuDocs> GetMenuDocs(int id)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                return iDbContext.DbContext.Query<V_MenuDocs>()
                    .Where(w => w.m_id == id)
                    .ToList();
            }
        }

        public int MenuAddDocs(int m_id,int d_id)
        {
            using(C_DbContext iDbContext = new C_DbContext())
            {
                MenuDocs idata = iDbContext.DbContext.Query<MenuDocs>()
                    .Where(w => w.m_id == m_id && w.d_id == d_id)
                    .FirstOrDefault();

                if (idata == null)
                {
                    idata = new MenuDocs();
                    idata.m_id = m_id;
                    idata.d_id = d_id;
                    idata.md_index = 0;
                    idata = iDbContext.DbContext.Insert<MenuDocs>(idata);
                    return idata == null ? 0 : 1;
                }
                else return 1;
            }
        }

        /// <summary>
        /// 删除指定的菜单
        /// <para>同步删除挂载信息</para>
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int MenuDel(int id)
        {
            using (C_DbContext iDbContext = new C_DbContext())
            {
                iDbContext.DbContext.Delete<MenuDocs>(w => w.m_id == id);
                return iDbContext.DbContext.Delete<menu>(w => w.m_id == id);
            }
        }
    }
}